#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 5e5 + 2;
typedef long long LL;
int jump[maxn][20];
int maxw[maxn][20];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &jump[i][0]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &maxw[i][0]);
     
  }
  for (int j = 1; (1 << j) <= k; j++) {
    for (int i = 1; i <= n; i++) {
      int next = jump[i][j - 1];
      jump[i][j] = jump[next][j - 1];
      maxw[i][j] = max(maxw[i][j - 1], maxw[next][j - 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    int p = i;
    int ans = INT_MIN;
    for (int j = 20; j >= 0; j--) {
      if (k & (1 << j)) {
        ans = max(ans, maxw[p][j]);
        p = jump[p][j];
      }
    }
    printf("%d\n", ans);
  }

  return 0;
}
